{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "98992aa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas import read_csv\n",
    "df = read_csv('td13_add_new.csv')\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bccd41e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Unnamed: 0                 数据时间   总有功功率 （kw）  天气状况  最高温度  最低温度  白天风力风向  \\\n",
      "0           0  2018-01-01 00:00:00  244033.7682     2    22    12       1   \n",
      "1           1  2018-01-01 00:15:00  232720.6402     2    22    12       1   \n",
      "2           2  2018-01-01 00:30:00  231685.6891     2    22    12       1   \n",
      "3           3  2018-01-01 00:45:00  227581.3317     2    22    12       1   \n",
      "4           4  2018-01-01 01:00:00  233482.4269     2    22    12       1   \n",
      "\n",
      "   夜晚风力风向  月  天  ...  一年第几天  一年第几周  weekday  时间段  季节  是否月初  是否月末  是否季节初  \\\n",
      "0       1  1  1  ...      1      1        0    1   1     1     0      1   \n",
      "1       1  1  1  ...      1      1        0    1   1     1     0      1   \n",
      "2       1  1  1  ...      1      1        0    1   1     1     0      1   \n",
      "3       1  1  1  ...      1      1        0    1   1     1     0      1   \n",
      "4       1  1  1  ...      1      1        0    1   1     1     0      1   \n",
      "\n",
      "   是否季节末  是否周末  \n",
      "0      0     0  \n",
      "1      0     0  \n",
      "2      0     0  \n",
      "3      0     0  \n",
      "4      0     0  \n",
      "\n",
      "[5 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cb1eea52",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb0646d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        Unnamed: 0                 数据时间   总有功功率 （kw）  天气状况  最高温度  最低温度  \\\n",
      "0                0  2018-01-01 00:00:00  244033.7682     2    22    12   \n",
      "1                1  2018-01-01 00:15:00  232720.6402     2    22    12   \n",
      "2                2  2018-01-01 00:30:00  231685.6891     2    22    12   \n",
      "3                3  2018-01-01 00:45:00  227581.3317     2    22    12   \n",
      "4                4  2018-01-01 01:00:00  233482.4269     2    22    12   \n",
      "...            ...                  ...          ...   ...   ...   ...   \n",
      "128539      128539  2021-08-31 22:45:00  202409.1270     9    32    26   \n",
      "128540      128540  2021-08-31 23:00:00  218576.4635     9    32    26   \n",
      "128541      128541  2021-08-31 23:15:00  200112.5091     9    32    26   \n",
      "128542      128542  2021-08-31 23:30:00  199865.5937     9    32    26   \n",
      "128543      128543  2021-08-31 23:45:00  198483.5592     9    32    26   \n",
      "\n",
      "        白天风力风向  夜晚风力风向  月   天  ...  一年第几天  一年第几周  weekday  时间段  季节  是否月初  \\\n",
      "0            1       1  1   1  ...      1      1        0    1   1     1   \n",
      "1            1       1  1   1  ...      1      1        0    1   1     1   \n",
      "2            1       1  1   1  ...      1      1        0    1   1     1   \n",
      "3            1       1  1   1  ...      1      1        0    1   1     1   \n",
      "4            1       1  1   1  ...      1      1        0    1   1     1   \n",
      "...        ...     ... ..  ..  ...    ...    ...      ...  ...  ..   ...   \n",
      "128539      25      22  8  31  ...    243     35        1    8   3     0   \n",
      "128540      25      22  8  31  ...    243     35        1    1   3     0   \n",
      "128541      25      22  8  31  ...    243     35        1    1   3     0   \n",
      "128542      25      22  8  31  ...    243     35        1    1   3     0   \n",
      "128543      25      22  8  31  ...    243     35        1    1   3     0   \n",
      "\n",
      "        是否月末  是否季节初  是否季节末  是否周末  \n",
      "0          0      1      0     0  \n",
      "1          0      1      0     0  \n",
      "2          0      1      0     0  \n",
      "3          0      1      0     0  \n",
      "4          0      1      0     0  \n",
      "...      ...    ...    ...   ...  \n",
      "128539     1      0      0     0  \n",
      "128540     1      0      0     0  \n",
      "128541     1      0      0     0  \n",
      "128542     1      0      0     0  \n",
      "128543     1      0      0     0  \n",
      "\n",
      "[128544 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "926d15a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=df.loc[:,['总有功功率 （kw）','天气状况','最高温度','最低温度','白天风力风向','夜晚风力风向','月','天','小时','一年第几天','一年第几周','weekday','时间段','季节','是否月初','是否月末','是否季节初','是否季节末','是否周末']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "089f254f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        天气状况  最高温度  最低温度  白天风力风向  夜晚风力风向  月   天  小时  一年第几天  一年第几周  weekday  \\\n",
      "0          2    22    12       1       1  1   1   0      1      1        0   \n",
      "1          2    22    12       1       1  1   1   0      1      1        0   \n",
      "2          2    22    12       1       1  1   1   0      1      1        0   \n",
      "3          2    22    12       1       1  1   1   0      1      1        0   \n",
      "4          2    22    12       1       1  1   1   1      1      1        0   \n",
      "...      ...   ...   ...     ...     ... ..  ..  ..    ...    ...      ...   \n",
      "128539     9    32    26      25      22  8  31  22    243     35        1   \n",
      "128540     9    32    26      25      22  8  31  23    243     35        1   \n",
      "128541     9    32    26      25      22  8  31  23    243     35        1   \n",
      "128542     9    32    26      25      22  8  31  23    243     35        1   \n",
      "128543     9    32    26      25      22  8  31  23    243     35        1   \n",
      "\n",
      "        时间段  季节  是否月初  是否月末  是否季节初  是否季节末  是否周末  \n",
      "0         1   1     1     0      1      0     0  \n",
      "1         1   1     1     0      1      0     0  \n",
      "2         1   1     1     0      1      0     0  \n",
      "3         1   1     1     0      1      0     0  \n",
      "4         1   1     1     0      1      0     0  \n",
      "...     ...  ..   ...   ...    ...    ...   ...  \n",
      "128539    8   3     0     1      0      0     0  \n",
      "128540    1   3     0     1      0      0     0  \n",
      "128541    1   3     0     1      0      0     0  \n",
      "128542    1   3     0     1      0      0     0  \n",
      "128543    1   3     0     1      0      0     0  \n",
      "\n",
      "[128544 rows x 18 columns]\n"
     ]
    }
   ],
   "source": [
    "X=df.loc[:,['天气状况','最高温度','最低温度','白天风力风向','夜晚风力风向','月','天','小时','一年第几天','一年第几周','weekday','时间段','季节','是否月初','是否月末','是否季节初','是否季节末','是否周末']]\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8be7dc93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['天气状况' '最高温度' '最低温度' '白天风力风向' '夜晚风力风向' '月' '天' '小时' '一年第几天' '一年第几周'\n",
      " 'weekday' '时间段' '季节' '是否月初' '是否月末' '是否季节初' '是否季节末' '是否周末']\n"
     ]
    }
   ],
   "source": [
    "names = np.array(['天气状况','最高温度','最低温度','白天风力风向','夜晚风力风向','月','天','小时','一年第几天','一年第几周','weekday','时间段','季节','是否月初','是否月末','是否季节初','是否季节末','是否周末'])\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f3310cbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Features sorted by their score:\n",
      "[(0.2916, '一年第几天'), (0.2194, '白天风力风向'), (0.1011, '时间段'), (0.0865, '最低温度'), (0.0737, '一年第几周'), (0.0608, '小时'), (0.0432, '夜晚风力风向'), (0.0412, '天'), (0.0295, '最高温度'), (0.022, 'weekday'), (0.0182, '天气状况'), (0.005, '月'), (0.0032, '是否周末'), (0.0018, '是否季节初'), (0.0018, '季节'), (0.0005, '是否月初'), (0.0004, '是否月末'), (0.0, '是否季节末')]\n"
     ]
    }
   ],
   "source": [
    "Y=df[\"总有功功率 （kw）\"]\n",
    "\n",
    "rf = RandomForestRegressor()\n",
    "rf.fit(X, Y)\n",
    "print(\"Features sorted by their score:\")\n",
    "print(sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_),names), reverse=True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
